package org.mbcorp.sar.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")
public class Solicitante extends Usuario implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Column(nullable = false)
	private String nombre;
	
	@Column(nullable = true)
	private String cargo;
		
	@JoinColumn(name = "cliente", referencedColumnName = "id")
	@ManyToOne(optional = false, fetch = FetchType.EAGER)
	private Cliente cliente;

	@OneToMany(mappedBy = "solicitante")
	@LazyCollection(LazyCollectionOption.FALSE)
	private List<Email> emails;
	
	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getCargo() {
		return cargo;
	}

	public void setCargo(String cargo) {
		this.cargo = cargo;
	}

	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	public List<Email> getEmails() {
		return emails;
	}

	public void setEmails(List<Email> emails) {
		this.emails = emails;
	}
	
	public Email getEmailPrincipal(){
		for(Email e: this.getEmails()){
			if(e.isFlagPrincipal()){
				return e;
			}
		}
		return null;
	}
}
